package com.carlinfo.userscenter.users.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.carlinfo.userscenter.common.util.PageInfoUtil;
import com.carlinfo.userscenter.users.dao.IAAdminsDao;
import com.carlinfo.userscenter.users.dao.IARoleDao;
import com.carlinfo.userscenter.users.dao.impl.AAdminsDaoImpl;
import com.carlinfo.userscenter.users.dao.impl.ARoleDaoImpl;
import com.carlinfo.userscenter.users.pojo.AAdmins;
import com.carlinfo.userscenter.users.pojo.ARole;
import com.carlinfo.userscenter.users.service.IUsersService;

public class UsersServiceImpl implements IUsersService
{
	/*
	 * =左边是父类
	 * =右边是子类
	 * 父类引用指向子类对象
	 */
	private IARoleDao roleDao = new ARoleDaoImpl();
	private IAAdminsDao adminsDao = new AAdminsDaoImpl();

	public JSONObject saveOneRoleService(ARole role)
	{
		JSONObject resultJSON = new JSONObject();
		/*（res)：Dao返回值指的是执行sql语句影响的条数*/
		int res = this.roleDao.saveOne(role);
		if (res > 0)
		{
			/*添加成功*/
			JSONObject dataJSON = new JSONObject();
			/*在json中，id是整数*/
			dataJSON.put("id", role.getId() + "");
			dataJSON.put("effect", res + "");
			/*将rsaultJSON和dataJSON关联起来*/
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
		} else
		{
			/*添加失败*/
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	public JSONObject updateOneRoleService(ARole role)
	{
		JSONObject resultJSON = new JSONObject();
		/*（res)：Dao返回值指的是执行sql语句影响的条数*/
		int res = this.roleDao.updateOne(role);	
		if (res > 0)
		{
			/*更新成功*/
			JSONObject dataJSON = new JSONObject();		
			dataJSON.put("effect", res + "");
			/* 将resultJSON和dataJSON关联起来 */
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
		} else
		{
			/*更新失败*/
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}

	public JSONObject deleteOneRoleService(ARole role)
	{
		JSONObject resultJSON = new JSONObject();
		/*（res)：Dao返回值指的是执行sql语句影响的条数*/
		int res = this.roleDao.deleteOne(role);
		if (res > 0)
		{
			/*删除成功*/
			JSONObject dataJSON = new JSONObject();

			dataJSON.put("effect", res + "");	
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
		} else
		{
			/*删除失败*/
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON;
	}

	public ARole findOneRoleService(Map<String, Object> condMap)
	{
		return this.roleDao.findOne(condMap);
	}

	public List<ARole> findCondListRoleService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		return this.roleDao.findCondList(pageInfoUtil, condMap);
	}

	public JSONObject savOneAdminsService(AAdmins admins)
	{		
		JSONObject resultJSON = new JSONObject();
		/*（res)：Dao返回值指的是执行sql语句影响的条数*/
		int res = this.adminsDao.saveOne(admins);
		if (res > 0)
		{
			/*添加成功*/
			JSONObject dataJSON = new JSONObject();		
			dataJSON.put("id", admins.getId() + "");
			dataJSON.put("effect", res + "");

			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
		} else
		{
			/*添加失败*/
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	public JSONObject updateOneAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject();
		/*（res)：Dao返回值指的是执行sql语句影响的条数*/
		int res = this.adminsDao.updateOne(admins);
		if (res > 0)
		{
			/*更新成功*/
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("effect", res + "");
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
		} else
		{
			/*更新失败*/
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}

	public JSONObject deleteOneAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject();
		/*（res)：Dao返回值指的是执行sql语句影响的条数*/
		int res = this.adminsDao.deleteOne(admins);
		if (res > 0)
		{
			/*删除成功*/
			JSONObject dataJSON = new JSONObject();		
			dataJSON.put("effect", res + "");
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
		} else
		{
			/*删除失败*/
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON;
	}

	public AAdmins findOneAdminsService(Map<String, Object> condMap)
	{

		AAdmins admins = this.adminsDao.findOne(condMap);
		/*关联关系的开关*/
		if ("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/*查询扩展表的条件*/
			Map<String, Object> extendMap = new HashMap<String, Object>();
			/*
			 * 根据id查询角色
			 * 通关管理员获取绝世id
			 * */
			extendMap.put("id", admins.getRoleId());
			ARole role = this.findOneRoleService(extendMap);
			admins.setRole(role);
		}
		return admins;
	}

	public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AAdmins> resultList = this.adminsDao.findCondList(pageInfoUtil, condMap);
		/*关联关系的开关*/
		if ("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/*查询扩展表的条件*/
			Map<String, Object> extendMap = new HashMap<String, Object>();
			for (Iterator iterator = resultList.iterator(); iterator.hasNext();)
			{
				AAdmins admins = (AAdmins) iterator.next();
				/*
				 * 根据id查询角色
				 * 通关管理员获取绝世id
				 * */
				extendMap.put("id", admins.getRoleId());
				ARole role = this.findOneRoleService(extendMap);
				admins.setRole(role);
			}
		}
		return resultList;
	}

}